package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.conn.JsonActivityAsyGet;
import com.zcx.qshop.entity.Good;

import java.util.List;

/**
 * Created by Administrator on 2015/11/9.
 */
public abstract class ActivitvAdapter extends ArrayAdapter<JsonActivityAsyGet.GoodDouble> {

    private LayoutInflater layoutInflater;

    private String type;

    public ActivitvAdapter(Context context, List<JsonActivityAsyGet.GoodDouble> objects, String type) {

        super(context, 0, objects);

        this.type = type;

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final JsonActivityAsyGet.GoodDouble goodDouble = getItem(position);

        ViewHolder viewHolder;

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_activitv, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (v.getId()){

                    case R.id.item_activitv_one:

                        onDetail(goodDouble.goodOne);

                        break;

                    case R.id.item_activitv_add_shop_car_one:

                        onShopCar(goodDouble.goodOne);

                        break;

                    case R.id.item_activitv_two:

                        onDetail(goodDouble.goodTwo);

                        break;

                    case R.id.item_activitv_add_shop_car_two:

                        onShopCar(goodDouble.goodTwo);

                        break;

                }

            }

        };

        Good good;

        if((good = goodDouble.goodOne) != null){

            viewHolder.one.setVisibility(View.VISIBLE);

            viewHolder.one.setOnClickListener(onClickListener);

            viewHolder.imgOne.setImageURI(Uri.parse(good.picurl));

            viewHolder.contentOne.setText(good.title);

            viewHolder.priceOne.setText(good.price + good.unit);

            switch (type){

                case "1":

                    viewHolder.countOne.setVisibility(View.VISIBLE);

                    viewHolder.countOne.setText("销量：" + good.number);

                    break;

                case "3":

                    viewHolder.originalOne.setVisibility(View.VISIBLE);

                    viewHolder.originalOne.setText("原价：" + good.markprice);

                    break;

                default:

                    viewHolder.addShopCarOne.setVisibility(View.VISIBLE);

                    viewHolder.addShopCarOne.setImageResource(true ? R.mipmap.app_add_shop_car_select : R.mipmap.app_add_shop_car);

                    viewHolder.addShopCarOne.setOnClickListener(onClickListener);

                    break;

            }

        }else{

            viewHolder.one.setVisibility(View.INVISIBLE);

        }

        if((good = goodDouble.goodTwo) != null){

            viewHolder.two.setVisibility(View.VISIBLE);

            viewHolder.two.setOnClickListener(onClickListener);

            viewHolder.imgTwo.setImageURI(Uri.parse(good.picurl));

            viewHolder.contentTwo.setText(good.title);

            viewHolder.priceTwo.setText(good.price + good.unit);

            switch (type){

                case "1":

                    viewHolder.countTwo.setVisibility(View.VISIBLE);

                    viewHolder.countTwo.setText("销量：" + good.number);

                    break;

                case "3":

                    viewHolder.originalTwo.setVisibility(View.VISIBLE);

                    viewHolder.originalTwo.setText("原价：" + good.markprice);

                    break;

                default:

                    viewHolder.addShopCarTwo.setVisibility(View.VISIBLE);

                    viewHolder.addShopCarTwo.setImageResource(true ? R.mipmap.app_add_shop_car_select : R.mipmap.app_add_shop_car);

                    viewHolder.addShopCarTwo.setOnClickListener(onClickListener);

                    break;

            }

        }else{

            viewHolder.two.setVisibility(View.INVISIBLE);

        }

        return convertView;

    }

    private class ViewHolder{

        @BoundView(R.id.item_activitv_one) private View one;

        @BoundView(R.id.item_activitv_img_one) private SimpleDraweeView imgOne;

        @BoundView(R.id.item_activitv_content_one) private TextView contentOne;

        @BoundView(R.id.item_activitv_price_one) private TextView priceOne;

        @BoundView(R.id.item_activitv_add_shop_car_one) private ImageView addShopCarOne;

        @BoundView(R.id.item_activitv_original_one) private TextView originalOne;

        @BoundView(R.id.item_activitv_count_one) private TextView countOne;

        @BoundView(R.id.item_activitv_two) private View two;

        @BoundView(R.id.item_activitv_img_two) private SimpleDraweeView imgTwo;

        @BoundView(R.id.item_activitv_content_two) private TextView contentTwo;

        @BoundView(R.id.item_activitv_price_two) private TextView priceTwo;

        @BoundView(R.id.item_activitv_add_shop_car_two) private ImageView addShopCarTwo;

        @BoundView(R.id.item_activitv_original_two) private TextView originalTwo;

        @BoundView(R.id.item_activitv_count_two) private TextView countTwo;

    }

    protected abstract void onDetail(Good good);

    protected abstract void onShopCar(Good good);

}
